<!--
2020-##-## 周# by xushenpeng
* IdmBaseSysinfoModal.vue
-->
<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="系统代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'SYSCODE', validatorRules.SYSCODE]" placeholder="请输入系统代码"></a-input>
        </a-form-item>
        <a-form-item label="系统名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'SYSNAME', validatorRules.SYSNAME]" placeholder="请输入系统名称"></a-input>
        </a-form-item>
        <a-form-item label="系统类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--<a-input v-decorator="[ 'systype', validatorRules.systype]" placeholder="请输入系统类型"></a-input>-->
          <j-dict-select-tag v-decorator="[ 'SYSTYPE', validatorRules.SYSTYPE]"
                             :trigger-change="true" dictCode="appl-type" placeholder="请选择系统类型"/>
        </a-form-item>
        <a-form-item label="网络类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--<a-input v-decorator="[ 'nettype', validatorRules.nettype]" placeholder="请输入网络类型"></a-input>-->
          <j-dict-select-tag type="list" v-decorator="[ 'NETTYPE', validatorRules.NETTYPE]"
                             :trigger-change="true" dictCode="appl-network-type" placeholder="请选择网络类型"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'MARK', validatorRules.MARK]" placeholder="请输入备注"></a-input>
        </a-form-item>
        <a-form-item label="图标" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['ICON', validatorRules.ICON]" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="连接方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'API', validatorRules.API]" placeholder="请输入连接方式"></a-input>
        </a-form-item>
        <a-form-item label="访问类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['APITYPE', validatorRules.APITYPE]"
                             :trigger-change="true" dictCode="appl-apitype" placeholder="请选择访问类型"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import store from '@/store/'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: "IdmBaseSysinfoModal",
    components: {
      JDictSelectTag,
      JUpload
    },
    props: ['tableName'],
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          SYSCODE: {rules: [
            {required: true, message: '请输入系统代码!'},
            { validator: (rule, value, callback) => validateDuplicateValue('idm_base_sysinfo', 'SYSCODE', value, this.model.ID, callback)},
          ]},
          SYSNAME: {rules: [
            {required: true, message: '请输入系统名称!'},
            { validator: (rule, value, callback) => validateDuplicateValue('idm_base_sysinfo', 'SYSNAME', value, this.model.ID, callback)},
          ]},
          SYSTYPE: {rules: [
          ]},
          NETTYPE: {rules: [
          ]},
          MARK: {rules: [
          ]},
          ICON: {rules: [
            // {required: true, message: '请输入图标!'},
          ]},
          API: {rules: [
          ]},
          APITYPE: {rules: [
            {required: true, message: '请输入访问类型!'},
          ]},
        },
        url: {
          // add: "/basesysinfo/idmBaseSysinfo/add",
          add: "/crud/insert",
          // edit: "/basesysinfo/idmBaseSysinfo/edit",
          edit: "/crud/update",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        // console.log("* record: ", record);
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          const modelPicked = pick(this.model,
              'SYSCODE',
              'SYSNAME',
              'SYSTYPE',
              'NETTYPE',
              'CREATEDATE',
              'CREATEUSER',
              'MODIFYDATE',
              'MODIFYUSER',
              'MARK',
              'ICON',
              'API',
              'APITYPE');
          // console.log("modelPicked: ", modelPicked);
          this.form.setFieldsValue(modelPicked);
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (err) {
            console.log("< form validation fail.")
            return;
          }
          // console.log("- this.model: ", this.model);
          // console.log("- values: ", values);
          console.log('- store.getters.userInfo: ', store.getters.userInfo)
          const reqBody = {table: this.tableName}, dataBody = {};
          if (this.model.ID) { // 修改
            Object.assign(dataBody, values
                , { modifydate: Date.now(), modifyuser: store.getters.userInfo.username });
            Object.assign(reqBody, {
              setClause: dataBody,
              where: [
                { or: [{ column: "id", operator: "eq", value: this.model.ID}]}
              ]
            });
          } else { // 新增
            Object.assign(dataBody, this.model, values
                , { createdate: Date.now(), createuser: store.getters.userInfo.username });
            Object.assign(reqBody, {inserts: dataBody});
          }
          Object.keys(dataBody).forEach(key => (dataBody[key] == null) && delete dataBody[key]);
          console.log("- reqBody: ", reqBody);
          postAction(this.model.ID ? this.url.edit : this.url.add, reqBody)
            .catch(err => console.log('axios err: ', err))
            .then(resp => {
              console.log(". resp: ", resp)
              if (resp.code !== 2000) {
                that.$message.warning(resp.msg);
                return;
              }
              that.$message.success("操作成功!");
              that.$emit('ok');
            })
            .finally(() => {
              that.confirmLoading = false;
              that.close();
            })
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,
            'SYSCODE',
            'SYSNAME',
            'SYSTYPE',
            'NETTYPE',
            'CREATEDATE',
            'CREATEUSER',
            'MODIFYDATE',
            'MODIFYUSER',
            'MARK',
            'ICON',
            'API',
            'APITYPE'))
      },
    }
  }
</script>